<html>
<head>
<script src="../resources/js-test.js"></script>
</head>
<body>

<p id="description"></p>

<button id="upper_target">Upper Target</button>
<div style="border: 1px solid #000; height: 5000px;">5000-pixel box</div>
<button id="lower_target">Lower Target</button>
<div style="border: 1px solid #000; height: 5000px;">5000-pixel box</div>

<div id="console"></div>

<script>
description("Tests that scrolling to make an element visible successfully scrolls the main window.");

function runTest() {
    var upperTarget = document.getElementById("upper_target");
    var lowerTarget = document.getElementById("lower_target");

    if (window.accessibilityController) {
        lowerTarget.focus();
        var lowerTargetAccessibleObject = accessibilityController.focusedElement;
        upperTarget.focus();
        var upperTargetAccessibleObject = accessibilityController.focusedElement;
    }

    // Reset the initial scroll position (since calling focus() can scroll the page too).
    window.scrollTo(0, 0);
    shouldBe("window.pageYOffset", "0");

    // Scroll to make lower target visible and check.
    if (window.accessibilityController)
        lowerTargetAccessibleObject.scrollToMakeVisible();
    window.minYOffset = lowerTarget.offsetTop + lowerTarget.offsetHeight - window.innerHeight;
    window.maxYOffset = lowerTarget.offsetTop;
    shouldBe("window.pageYOffset >= minYOffset", "true");
    shouldBe("window.pageYOffset <= maxYOffset", "true");

    // Scroll to make upper target visible and check.
    if (window.accessibilityController)
        upperTargetAccessibleObject.scrollToMakeVisible();
    window.minYOffset = upperTarget.offsetTop + upperTarget.offsetHeight - window.innerHeight;
    window.maxYOffset = upperTarget.offsetTop;
    shouldBe("window.pageYOffset >= minYOffset", "true");
    shouldBe("window.pageYOffset <= maxYOffset", "true");

    finishJSTest();
}

runTest();

</script>

</body>
</html>
